package com.sparkseries.service.impl;

import com.sparkseries.dao.SysDepartmentMapper;
import com.sparkseries.domain.SysDepartment;
import com.sparkseries.domain.vo.SysDepartmentVO;
import com.sparkseries.dto.SysDepartmentDTO;
import com.sparkseries.service.SysDepartmentService;
import com.sparkseries.common.util.domain.Result;
import com.sparkseries.common.util.tool.ObjectConvertTool;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 部门管理
 */
@Service
public class SysDepartmentServiceImpl implements SysDepartmentService {
    @Resource
    private SysDepartmentMapper dao;

    @Override
    public Result<?> createDepartments(List<SysDepartmentDTO> sysDepartmentsDTO) {
        ArrayList<SysDepartmentDTO> existedLists = new ArrayList<>();
        ArrayList<SysDepartmentDTO> lists = new ArrayList<>();
        if (sysDepartmentsDTO == null || sysDepartmentsDTO.isEmpty()) {
            return Result.error("你没有输入数据");
        } else {
            for (SysDepartmentDTO sysDepartment : sysDepartmentsDTO) {
                if (dao.getDepartmentById(sysDepartment.getDepartmentId()) == null) {
                    lists.add(sysDepartment);
                } else {
                    existedLists.add(sysDepartment);
                }
            }
            if (sysDepartmentsDTO.size() == lists.size()) {
                List<SysDepartment> departments = ObjectConvertTool.originsConvert(sysDepartmentsDTO, SysDepartment.class);
                if (dao.insertDepartments(departments) != 0) {
                    return Result.ok("创建成功");
                } else {
                    return Result.error("创建失败");
                }
            } else if (sysDepartmentsDTO.size() == existedLists.size()) {
                return Result.error("你想要创建的部门id全部已经被使用了创建失败:");
            } else {
                List<SysDepartment> vos = ObjectConvertTool.originsConvert(lists, SysDepartment.class);
                if (dao.insertDepartments(vos) != 0) {
                    return Result.ok("你创建的部门中有一部分部门id已经被使用了无法再创建 已存在部门:" + existedLists);
                } else {
                    return Result.error("创建失败");
                }
            }
        }
    }

    /**
     * 获取所有部门
     *
     * @return
     */
    @Override
    public Result<List<SysDepartmentVO>> getAllDepart() {
        List<SysDepartment> allDepartments = dao.getAllDepartments();
        if (allDepartments != null) {
            return Result.ok("获取成功", ObjectConvertTool.originsConvert(allDepartments, SysDepartmentVO.class));
        } else {
            return Result.error("获取失败");
        }
    }

    /**
     * 删除部门
     *
     * @param ids
     * @return
     */
    @Override
    public Result<?> deleteDepart(List<Integer> ids) {
        if (ids == null || ids.isEmpty()) {
            return Result.error("你没有输入id");
        } else {
            if (dao.deleteDepartment(ids) != 0) {
                return Result.ok("删除成功");
            } else {
                return Result.error("删除失败");
            }
        }

    }

    /**
     * 修改部门
     *
     * @param sysDepartment
     * @return
     */
    @Override
    public Result<?> updateDepart(SysDepartmentDTO sysDepartment) {
        if (sysDepartment == null) {
            return Result.error("你没有提交有效的数据");
        } else {
            if (dao.updateDepartment(ObjectConvertTool.originConvert(sysDepartment, SysDepartment.class)) != 0) {
                return Result.ok("修改成功");
            } else {
                return Result.error("你想要修改的部门不存在");
            }
        }
    }

    /**
     * 通过id获取部门
     *
     * @param id
     * @return
     */
    @Override
    public Result<SysDepartmentVO> getDepartById(Integer id) {
        if (id == null) {
            return Result.error("你没有输入id");
        } else {
            SysDepartment sysDepartment = dao.getDepartmentById(id);
            if (sysDepartment != null) {
                return Result.ok("获取成功",ObjectConvertTool.originConvert(sysDepartment, SysDepartmentVO.class));
            } else {
                return Result.error("你输入的id不存在");
            }
        }

    }

    @Override
    public Result<List<SysDepartmentVO>> getDirectChildren(Integer id) {
        if (id == null) {
            return Result.error("你没有输入id");
        } else {
            List<SysDepartment> sysDepartments = dao.getDirectChildren(id);
            List<SysDepartmentVO> sysDepartmentVO = ObjectConvertTool.originsConvert(sysDepartments, SysDepartmentVO.class);
            return Result.ok("获取成功", sysDepartmentVO);
        }

    }

    @Override
    public Result<List<SysDepartmentVO>> findDepartmentByCondition(SysDepartmentDTO sysDepartmentDTO) {
        List<SysDepartment> departmentByCondition = dao.findDepartmentByCondition(ObjectConvertTool.originConvert(sysDepartmentDTO, SysDepartment.class));
        if (departmentByCondition.isEmpty()) {
            return Result.error("你输入的条件没有匹配到任何部门");
        } else {
            return Result.ok("获取成功",ObjectConvertTool.originsConvert(departmentByCondition, SysDepartmentVO.class));
        }
    }

}
